﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThaiHoa.Win.QLSX.Entity;
using ThaiHoa.Win.QLSX.Kho.UTI;
using ThaiHoa.Win.QLSX.DAL;
using Excel = Microsoft.Office.Interop.Excel;

namespace ThaiHoa.Win.QLSX.Kho.UI
{
    public partial class frmThepLuoiList : Form
    {
        #region Fields
        KhoDBContext db;

        BindingSource LuoiBS;
        Access acs;
        frmVatTuSearch _frmVatTuSearch;

        #endregion
        #region Constructor
        public frmThepLuoiList()
        {
            InitializeComponent();
        }
        #endregion
        #region Functions
        private void KhoiTao()
        {
            db = new KhoDBContext();
            db.Saved += new EventHandler(db_Saved);
            LuoiBS = new BindingSource();
            acs = new Access();
            db.Luoi.Include("LoaiLuoi").Include("LoaiVatTu").Where(m => m.TrangThai).Load();
            LuoiBS.DataSource = db.Luoi.Local.ToBindingList();
            dgvLuoi.DataSource = LuoiBS;
            LuoiBN.BindingSource = LuoiBS;
            CustomerGridView();
        }

        private void TimKiem(SearchVatTuEventArgs e)
        {
            db = new KhoDBContext();
            db.Saved += new EventHandler(db_Saved);
            LuoiBS = new BindingSource();
            acs = new Access();
            db.Luoi.Include("LoaiLuoi").Include("LoaiVatTu")
                .Where(
                    m => m.TrangThai
                    && m.MaMatHang.ToLower().Contains(e.Ma)
                    && m.TenMatHang.ToLower().Contains(e.Ten)
                    && m.LoaiLuoi.Id.Equals(e.MacVatTu.Tenmac.Equals("Tất Cả") ? m.LoaiLuoi.Id : e.MacVatTu.Id)
                )
                .Load();
            LuoiBS.DataSource = db.Luoi.Local.ToBindingList();
            dgvLuoi.DataSource = LuoiBS;
            LuoiBN.BindingSource = LuoiBS;
            CustomerGridView();
        }

        private void ReLoadData()
        {
            if (db != null) db.Dispose();
            KhoiTao();
        }

        private void CustomerGridView()
        {
            //Ẩn toàn bộ columns
            foreach (DataGridViewColumn item in dgvLuoi.Columns)
            {
                item.Visible = false;
            }
            //Tùy chỉnh lại
            //Mã
            var c = dgvLuoi.Columns["MaMatHang"];
            c.Visible = true;
            c.Width = 200;
            c.DisplayIndex = 1;

            //Tên
            c = dgvLuoi.Columns["TenMatHang"];
            c.Visible = true;
            c.Width = 200;
            c.DisplayIndex = 2;

            //mác thép
            c = dgvLuoi.Columns["LoaiLuoi"];
            c.Visible = true;
            c.Width = 100;
            c.DisplayIndex = 3;
            c.ReadOnly = true;

            //Kích Thước cuộn
            c = dgvLuoi.Columns["KichThuocCuonM"];
            c.Visible = true;
            c.Width = 100;
            c.DisplayIndex = 4;

            //Chiều rộng dải cắt
            c = dgvLuoi.Columns["ChieuRongDaiCat"];
            c.Visible = true;
            c.Width = 100;
            c.DisplayIndex = 5;

            //Số chi tiết/ dải cắt
            c = dgvLuoi.Columns["SoChiTietPerDaiCat"];
            c.Visible = true;
            c.Width = 100;
            c.DisplayIndex = 6;

            //Số dải cắt
            c = dgvLuoi.Columns["SoDaiCat"];
            c.Visible = true;
            c.Width = 100;
            c.DisplayIndex = 7;

            //Số chi tiết/cuộn
            c = dgvLuoi.Columns["SoChiTietPerCuon"];
            c.Visible = true;
            c.Width = 100;
            c.DisplayIndex = 8;

            //Số chi tiết thực tế / cuộn
            c = dgvLuoi.Columns["SoChiTietThucTePerCuon"];
            c.Visible = true;
            c.Width = 100;
            c.DisplayIndex = 9;

            //Tỷ lệ NG
            c = dgvLuoi.Columns["TyLeNG"];
            c.Visible = true;
            c.Width = 100;
            c.DisplayIndex = 10;
        }

        private void HienThiChiTiet()
        {
            Luoi item = LuoiBS.Current as Luoi;
            if (item != null)
            {
                frmThepLuoiDetail _frmLuoiDetail = new frmThepLuoiDetail();
                _frmLuoiDetail.DBChanged += new EventHandler(_frmLuoiDetail_DBChanged);
                acs.ShowFormThepLuoiDetail(_frmLuoiDetail, item.Id);
            }
        }

        private void Xoa()
        {
            Luoi item = LuoiBS.Current as Luoi;
            if (Common.ShowMessengerConfirmDelete() == DialogResult.Yes)
            {
                if (item != null)
                {
                    db.Luoi.Remove(item);
                    db.SaveChanges();
                }
            }
        }

        private void LuuLai()
        {
            if (Common.ShowMessengerConfirmSave() == DialogResult.Yes)
            {
                db.SaveChanges();
            }
        }
        private void ExportToExcel()
        {

            //string workbookPath = Application.StartupPath + @"\Template\Excel\JAT_VatTu_DanhMucLuoi";
            //Excel.Application xlApp = new Excel.Application();
            //Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(workbookPath,
            //   0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "",
            //    true, false, 0, true, false, false);
            //Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[1];
            //xlWorkBook.Activate();
            //xlWorkSheet.Name = "Luoi";
            ////Add data
            //var listLuoi = LuoiBS.List;
            //int count = 0;
            //int positonStart = 8;
            //Excel.Range r;
            //foreach (Luoi item in listLuoi)
            //{
            //    xlWorkSheet.Cells[positonStart + count, 1] = count + 1;
            //    xlWorkSheet.Cells[positonStart + count, 2] = item.MaMatHang;
            //    xlWorkSheet.Cells[positonStart + count, 3] = item.TenMatHang;
            //    xlWorkSheet.Cells[positonStart + count, 4] = item.MacThep.Tenmac;
            //    xlWorkSheet.Cells[positonStart + count, 5] = item.DuongKinhPhoi;
            //    xlWorkSheet.Cells[positonStart + count, 6] = item.ChieuDaiPhoi;
            //    xlWorkSheet.Cells[positonStart + count, 7] = item.SoChiTietPerCay;
            //    xlWorkSheet.Cells[positonStart + count, 8] = item.ChieuDaiCay;
            //    xlWorkSheet.Cells[positonStart + count, 9] = item.TrongLuongCayKG;
            //    xlWorkSheet.Cells[positonStart + count, 10] = item.TrongLuongPhoiPerChiTiet;
            //    xlWorkSheet.Cells[positonStart + count, 11] = item.TyLeNG;

            //    //Format it               

            //    string start = "A" + (count + positonStart).ToString();
            //    string stop = "K" + (count + positonStart).ToString();
            //    r = (Excel.Range)xlWorkSheet.get_Range(start, stop);
            //    r.Borders.Color = Color.Black;
            //    r.RowHeight = 30;
            //    //r.Style.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
            //    r.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
            //    count++;

            //}
            //// Hiện chương trình
            //xlApp.Visible = true;

            ////// Save file
            ////xlWorkBook.SaveAs("C:\\JAT_VatTu_DanhMucLuoi_1.xls", Excel.XlFileFormat.xlWorkbookNormal,
            ////                null, null, false, false,
            ////                Excel.XlSaveAsAccessMode.xlExclusive,
            ////                false, false, false, false, false);
            //xlApp.Quit();
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

            string workbookPath = Application.StartupPath + @"\Template\Excel\JAT_VatTu_DanhMucThepLuoi";
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(workbookPath,
               0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "",
                true, false, 0, true, false, false);
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[1];
            xlWorkBook.Activate();
            xlWorkSheet.Name = "Luoi";
            //Add data
            var listLuoi = LuoiBS.List;
            int count = 0;
            int postionStart = 6;
            Excel.Range r;
            foreach (Luoi item in listLuoi)
            {
                xlWorkSheet.Cells[postionStart + count, 1] = count + 1;
                xlWorkSheet.Cells[postionStart + count, 2] = item.MaMatHang;
                xlWorkSheet.Cells[postionStart + count, 3] = item.TenMatHang;
                xlWorkSheet.Cells[postionStart + count, 4] = item.LoaiLuoi.Tenmac;
                xlWorkSheet.Cells[postionStart + count, 5] = item.KichThuocCuonM;
                xlWorkSheet.Cells[postionStart + count, 6] = item.ChieuRongDaiCat;
                xlWorkSheet.Cells[postionStart + count, 7] = item.SoChiTietPerDaiCat;
                xlWorkSheet.Cells[postionStart + count, 8] = item.SoDaiCat;
                xlWorkSheet.Cells[postionStart + count, 9] = item.SoChiTietPerCuon;
                xlWorkSheet.Cells[postionStart + count, 10] = item.SoChiTietThucTePerCuon;
                xlWorkSheet.Cells[postionStart + count, 11] = item.TyLeNG;

                //Format it               

                string start = "A" + (count + postionStart).ToString();
                string stop = "K" + (count + postionStart).ToString();
                r = (Excel.Range)xlWorkSheet.get_Range(start, stop);
                r.Borders.Color = Color.Black;
                r.RowHeight = 30;
                //r.Style.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                r.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
                count++;

            }
            // Hiện
            xlApp.Visible = true;

            //// Save file
            //xlWorkBook.SaveAs("C:\\JAT_VatTu_DanhMucThepLuoi_1.xls", Excel.XlFileFormat.xlWorkbookNormal,
            //                null, null, false, false,
            //                Excel.XlSaveAsAccessMode.xlExclusive,
            //                false, false, false, false, false);
            xlApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
        }
        #endregion
        #region Events

        private void frmLuoiList_Load(object sender, EventArgs e)
        {
            KhoiTao();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            frmThepLuoiDetail _frmLuoiDetail = new frmThepLuoiDetail();
            _frmLuoiDetail.DBChanged += new EventHandler(_frmLuoiDetail_DBChanged);
            acs.ShowFormThepLuoiDetail(_frmLuoiDetail, Guid.Empty);
        }



        private void btnInformation_Click(object sender, EventArgs e)
        {
            HienThiChiTiet();
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            ReLoadData();
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            Xoa();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvLuoi_DoubleClick(object sender, EventArgs e)
        {
            HienThiChiTiet();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            LuuLai();
        }

        void db_Saved(object sender, EventArgs e)
        {
            ReLoadData();
        }
        void _frmLuoiDetail_DBChanged(object sender, EventArgs e)
        {
            ReLoadData();
        }
        private void btnExport_Click(object sender, EventArgs e)
        {
            ExportToExcel();
        }
        private void btnFind_Click(object sender, EventArgs e)
        {
            _frmVatTuSearch = new frmVatTuSearch();
            _frmVatTuSearch.Searched += new EventHandler<SearchVatTuEventArgs>(_frmVatTuSearch_Searched);
            _frmVatTuSearch.ShowDialog();
        }

        void _frmVatTuSearch_Searched(object sender, SearchVatTuEventArgs e)
        {
            TimKiem(e);
        }

        private void dgvLuoi_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            Common.ShowMessengerError("Dữ liệu nhập vào không hợp lệ !");
        }
        #endregion






    }
}
